﻿using PhysicalExamination5.Model.DataMaintenance;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using PhysicalExamination5.Model.Extensions;
using System.Runtime.Intrinsics.Arm;
using PhysicalExamination5.Interface.DataMaintenance;
using PhysicalExamination5.Model.PhysicalExamination;
using static System.Net.Mime.MediaTypeNames;
using System.Security.Cryptography;

namespace PhysicalExamination5.Service.DataMaintenance
{
    public class STaoCanWeiHu : ITaoCanWeiHu
    {
        private readonly ISqlSugarClient db;
        public STaoCanWeiHu(ISqlSugarClient db)
        {
            this.db = db;
        }

        #region 获取数据
        /// <summary>
        /// 获取套餐主
        /// </summary>
        /// <param name="getTaoCanChaXun"></param>
        /// <returns></returns>
        public string GetTaoCanZhuList(GetTaoCanChaXun getTaoCanChaXun)
        {
            //返回值
            var strResult = "";
            //记录日志
            Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{JsonConvert.SerializeObject(getTaoCanChaXun)}】");
            try
            {
                var strSql = new StringBuilder();
                strSql.Append($@"
                    SELECT * FROM jcsj_TaoCanZhu WITH(NOLOCK) WHERE 1=1
                ");
                if (!getTaoCanChaXun.IsEmpty())
                {
                    //套餐名称
                    if (!getTaoCanChaXun.TaoCanZhuMingCheng.IsEmpty())
                    {
                        strSql.Append($@"
                            AND TaoCanZhuMingCheng = '{getTaoCanChaXun.TaoCanZhuMingCheng}'
                        ");
                    }
                    //套餐院区
                    if (!getTaoCanChaXun.SuoShuYuanQu.IsEmpty())
                    {
                        strSql.Append($@"
                            AND SuoShuYuanQu LIKE '%{getTaoCanChaXun.SuoShuYuanQu}%'
                        ");
                    }
                    //套餐时间
                    if (!getTaoCanChaXun.TaoCanZhiDingShiJian.IsEmpty())
                    {
                        var startTime = Convert.ToDateTime(getTaoCanChaXun.TaoCanZhiDingShiJian).ToString("yyyy-MM-dd") + " 00:00:00";
                        var endTime = Convert.ToDateTime(getTaoCanChaXun.TaoCanZhiDingShiJian).ToString("yyyy-MM-dd") + " 23:59:59";
                        strSql.Append($@"
                            AND TaoCanZhiDingShiJian BETWEEN  '{startTime}' AND '{endTime}'
                        ");
                    }
                }
                List<jcsj_TaoCanZhu> list = new SqlSugarRepository<jcsj_TaoCanZhu>(db).getListBySql(strSql.ToString());
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 0,
                    Message = "查询成功!",
                    Data = list
                });
            }
            catch (Exception ex)
            {
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
            return strResult;
        }
        /// <summary>
        /// 获取套餐主详情
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public string GetTaoCanZhuEntity(string tid)
        {
            //返回值
            var strResult = "";
            //记录日志
            Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{JsonConvert.SerializeObject(tid)}】");
            try
            {
                //判断输入参数
                if (tid.IsEmpty())
                {
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 1,
                        Message = "套餐tid为空，请检查！"
                    });
                }
                else
                {
                    jcsj_TaoCanZhu jcsj_TaoCanZhu = new SqlSugarRepository<jcsj_TaoCanZhu>(db).getByWhere(t => t.tid == Int32.Parse(tid))?.FirstOrDefault();
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 0,
                        Message = "查询成功!",
                        Data = jcsj_TaoCanZhu
                    });
                }
            }
            catch (Exception ex)
            {
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
            return strResult;
        }
        /// <summary>
        /// 获取套餐主下的套餐
        /// </summary>
        /// <param name="TaoCanZhuID"></param>
        /// <returns></returns>
        public string GetTaoCanList(string TaoCanZhuID)
        {
            //返回值
            var strResult = "";
            //记录日志
            Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{TaoCanZhuID}】");
            try
            {
                //判断输入参数
                if (TaoCanZhuID.IsEmpty())
                {
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 1,
                        Message = "套餐主tid为空，请检查！"
                    });
                }
                else
                {
                    //判断该套餐主是否存在套餐
                    List<jcsj_TaoCan> jcsj_TaoCans = new SqlSugarRepository<jcsj_TaoCan>(db).getByWhere(t => t.TaoCanZhuID == TaoCanZhuID).ToList();
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 0,
                        Message = "查询成功!",
                        Data = jcsj_TaoCans
                    });
                }
            }
            catch (Exception ex)
            {
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
            return strResult;
        }
        /// <summary>
        ///  获取套餐下的项目
        /// </summary>
        /// <param name="TaoCanID"></param>
        /// <returns></returns>
        public string GetTaoCanXiangMuList(string TaoCanID)
        {
            //返回值
            var strResult = "";
            //记录日志
            Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{TaoCanID}】");
            try
            {
                //判断输入参数
                if (TaoCanID.IsEmpty())
                {
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 1,
                        Message = "套餐ID为空，请检查！"
                    });
                }
                else
                {
                    List<jcsj_TaoCanXiangMu> list = new SqlSugarRepository<jcsj_TaoCanXiangMu>(db).getListBySql($@"
                        SELECT * FROM jcsj_TaoCanXiangMu 
                        WITH(NOLOCK) WHERE TaoCanID = '{TaoCanID}'
                        ORDER BY ShunXu ASC
                    ");
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 0,
                        Message = "查询成功!",
                        Data = list
                    });
                }
            }
            catch (Exception ex)
            {
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
            return strResult;
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存套餐主
        /// </summary>
        /// <param name="tid"></param>
        /// <param name="SaveTaoCanZhu"></param>
        /// <param name="strYuanQuQianZhui"></param>
        /// <returns></returns>
        public string SaveTaoCanZhuData(string? tid, SaveTaoCanZhuHelp SaveTaoCanZhu, string strYuanQuQianZhui)
        {
            //返回值
            var strResult = "";
            //记录日志
            Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{JsonConvert.SerializeObject(new
            {
                tid = tid,
                jcsj_TaoCanZhu = SaveTaoCanZhu,
                strYuanQuQianZhui = strYuanQuQianZhui
            })}】");
            try
            {
                //新增
                if (tid.IsEmpty())
                {
                    jcsj_TaoCanZhu jcsj_TaoCanZhu = new jcsj_TaoCanZhu();
                    Extensions.ModelCopy(SaveTaoCanZhu, jcsj_TaoCanZhu);
                    jcsj_TaoCanZhu.TaoCanZhiDingShiJian = DateTime.Now;
                    List<jcsj_TaoCanZhu> jcsj_TaoCanZhus = new SqlSugarRepository<jcsj_TaoCanZhu>(db).getAll();
                    if (jcsj_TaoCanZhus.Any())
                    {
                        jcsj_TaoCanZhu.ShunXu = jcsj_TaoCanZhus.Max(t => t.ShunXu.Value) + 1;
                    }
                    else
                    {
                        jcsj_TaoCanZhu.ShunXu = 1;
                    }
                    int newtid = new SqlSugarRepository<jcsj_TaoCanZhu>(db).AddReturnIdentity(jcsj_TaoCanZhu);
                    //赋值套餐主Id
                    new SqlSugarRepository<jcsj_TaoCanZhu>(db).Update(t => new jcsj_TaoCanZhu
                    {
                        TaoCanZhuID = strYuanQuQianZhui + newtid.ToString()
                    }, t => t.tid == newtid);
                }
                //修改
                else
                {
                    new SqlSugarRepository<jcsj_TaoCanZhu>(db).Update(t => new jcsj_TaoCanZhu
                    {
                        TaoCanZhuMingCheng = SaveTaoCanZhu.TaoCanZhuMingCheng,
                        SuoShuYuanQu = SaveTaoCanZhu.SuoShuYuanQu,
                        PYM = SaveTaoCanZhu.PYM,
                    }, t => t.tid == Int32.Parse(tid));
                }
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 0,
                    Message = "保存成功",
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
            return strResult;
        }
        /// <summary>
        /// 删除套餐主
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public string DeleteTaoCanZhuData(string tid)
        {
            //返回值
            var strResult = "";
            //记录日志
            Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{JsonConvert.SerializeObject(tid)}】");
            try
            {
                //判断输入参数
                if (tid.IsEmpty())
                {
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 1,
                        Message = "套餐tid为空，请检查！"
                    });
                }
                else
                {
                    jcsj_TaoCanZhu jcsj_TaoCanZhu = new SqlSugarRepository<jcsj_TaoCanZhu>(db).getFirstOrDefault(A => A.tid == Int32.Parse(tid));
                    //判断该套餐主是否存在套餐
                    int TaoCanCount = new SqlSugarRepository<jcsj_TaoCan>(db).getByWhere(t => t.TaoCanZhuID == jcsj_TaoCanZhu.TaoCanZhuID).Count();
                    if (TaoCanCount > 0)
                    {
                        strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                        {
                            Success = 1,
                            Message = "存在套餐不允许删除！"
                        });
                    }
                    else
                    {
                        new SqlSugarRepository<jcsj_TaoCanZhu>(db).DeleteWhere(A => A.tid == Int32.Parse(tid));
                        strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                        {
                            Success = 0,
                            Message = "删除成功!"
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
            return strResult;
        }
        /// <summary>
        /// 套餐保存
        /// </summary>
        /// <param name="TaoCanZhuID"></param>
        /// <param name="strYuanQuQianZhui"></param>
        /// <param name="saveTaoCanHelp"></param>
        /// <returns></returns>
        public string SaveTaoCanData(string TaoCanZhuID, string strYuanQuQianZhui, SaveTaoCanHelp saveTaoCanHelp)
        {
            //返回值
            var strResult = "";
            //记录日志
            Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{JsonConvert.SerializeObject(new
            {
                TaoCanZhuID = TaoCanZhuID,
                saveTaoCanHelp = saveTaoCanHelp
            })}】");
            try
            {
                //判断输入参数
                if (TaoCanZhuID.IsEmpty())
                {
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 1,
                        Message = "套餐主Id为空，请检查！"
                    });
                }
                else
                {
                    #region 套餐信息
                    jcsj_TaoCan jcsj_TaoCan = new jcsj_TaoCan();
                    Extensions.ModelCopy(saveTaoCanHelp.taoCan, jcsj_TaoCan);
                    jcsj_TaoCan.TaoCanZhiDingShiJian = DateTime.Now;
                    jcsj_TaoCan.TaoCanZhuID = TaoCanZhuID;
                    List<jcsj_TaoCan> jcsj_TaoCans = new SqlSugarRepository<jcsj_TaoCan>(db).getAll();
                    if (jcsj_TaoCans.Any())
                    {
                        jcsj_TaoCan.ShunXu = jcsj_TaoCans.Max(t => t.ShunXu) + 1;
                    }
                    else
                    {
                        jcsj_TaoCan.ShunXu = 1;
                    }
                    int newtid = new SqlSugarRepository<jcsj_TaoCan>(db).AddReturnIdentity(jcsj_TaoCan);
                    //赋值套餐主Id
                    new SqlSugarRepository<jcsj_TaoCan>(db).Update(t => new jcsj_TaoCan
                    {
                        TaoCanID = strYuanQuQianZhui + newtid.ToString()
                    }, t => t.tid == newtid);
                    jcsj_TaoCan.TaoCanID = strYuanQuQianZhui + newtid;
                    #endregion

                    #region 套餐项目
                    List<jcsj_TaoCanXiangMu> jcsj_TaoCanXiangMus = new List<jcsj_TaoCanXiangMu>();
                    foreach (var item in saveTaoCanHelp.taoCanXiangMuHelp)
                    {
                        jcsj_TaoCanXiangMu jcsj_TaoCanXiangMu = new jcsj_TaoCanXiangMu();
                        Extensions.ModelCopy(item, jcsj_TaoCanXiangMu);
                        jcsj_TaoCanXiangMus.Add(jcsj_TaoCanXiangMu);
                    }
                    for (int i = 0; i < jcsj_TaoCanXiangMus.Count; i++)
                    {
                        jcsj_TaoCanXiangMus[i].TaoCanID = jcsj_TaoCan.TaoCanID;
                        jcsj_TaoCanXiangMus[i].ShunXu = i + 1;
                        new SqlSugarRepository<jcsj_TaoCanXiangMu>(db).Add(jcsj_TaoCanXiangMus[i]);
                    }
                    #endregion

                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 0,
                        Message = "保存成功",
                        Data = ""
                    });
                }
            }
            catch (Exception ex)
            {
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
            return strResult;
        }
        /// <summary>
        /// 删除套餐
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public string DeleteTaoCanData(string tid)
        {
            //返回值
            var strResult = "";
            //记录日志
            Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{JsonConvert.SerializeObject(tid)}】");
            try
            {
                //判断输入参数
                if (tid.IsEmpty())
                {
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 1,
                        Message = "套餐tid为空，请检查！"
                    });
                }
                else
                {
                    jcsj_TaoCan jcsj_TaoCan = new SqlSugarRepository<jcsj_TaoCan>(db).getFirstOrDefault(A => A.tid == Int32.Parse(tid));
                    //判断该套餐主是否存在套餐
                    int TaoCanCount = new SqlSugarRepository<jcsj_TaoCanXiangMu>(db).getByWhere(t => t.TaoCanID == jcsj_TaoCan.TaoCanID).Count();
                    if (TaoCanCount > 0)
                    {
                        strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                        {
                            Success = 1,
                            Message = "存在套餐项目不允许删除！"
                        });
                    }
                    else
                    {
                        //删除套餐
                        new SqlSugarRepository<jcsj_TaoCan>(db).DeleteWhere(A => A.tid == Int32.Parse(tid));
                        strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                        {
                            Success = 0,
                            Message = "删除成功!"
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
            return strResult;
        }
        /// <summary>
        /// 修改套餐下的项目
        /// </summary>
        /// <param name="TaoCanID"></param>
        /// <param name="updateTaoCan"></param>
        /// <returns></returns>
        public string UpdateTaoCanData(string TaoCanID, UpdateTaoCanHelp updateTaoCan)
        {
            //返回值
            var strResult = "";
            //记录日志
            Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:接收参数【{JsonConvert.SerializeObject(new
            {
                TaoCanID = TaoCanID,
                updateTaoCan = updateTaoCan
            })}】");
            try
            {
                //判断输入参数
                if (TaoCanID.IsEmpty())
                {
                    strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                    {
                        Success = 1,
                        Message = "套餐ID为空，请检查！"
                    });
                }
                else
                {
                    jcsj_TaoCan jcsj_TaoCan = new SqlSugarRepository<jcsj_TaoCan>(db).getFirstOrDefault(A => A.TaoCanID == TaoCanID);
                    //修改价格
                    new SqlSugarRepository<jcsj_TaoCan>(db).Update(t => new jcsj_TaoCan
                    {
                        TaoCanYuanJia = updateTaoCan.taoCanJiaGe.TaoCanYuanJia,
                        YouHuiJia = updateTaoCan.taoCanJiaGe.YouHuiJia,
                        ZheKou = updateTaoCan.taoCanJiaGe.ZheKou,
                    }, t => t.tid == jcsj_TaoCan.tid);
                    //先删除
                    new SqlSugarRepository<jcsj_TaoCanXiangMu>(db).DeleteWhere(A => A.TaoCanID == jcsj_TaoCan.TaoCanID);
                    //重新保存项目
                    List<jcsj_TaoCanXiangMu> jcsj_TaoCanXiangMus = new List<jcsj_TaoCanXiangMu>();
                    foreach (var item in updateTaoCan.taoCanXiangMuHelp)
                    {
                        jcsj_TaoCanXiangMu jcsj_TaoCanXiangMu = new jcsj_TaoCanXiangMu();
                        Extensions.ModelCopy(item, jcsj_TaoCanXiangMu);
                        jcsj_TaoCanXiangMus.Add(jcsj_TaoCanXiangMu);
                    }
                    for (int i = 0; i < jcsj_TaoCanXiangMus.Count; i++)
                    {
                        jcsj_TaoCanXiangMus[i].TaoCanID = jcsj_TaoCan.TaoCanID;
                        jcsj_TaoCanXiangMus[i].ShunXu = i + 1;
                        new SqlSugarRepository<jcsj_TaoCanXiangMu>(db).Add(jcsj_TaoCanXiangMus[i]);
                    }
                }
            }
            catch (Exception ex)
            {
                strResult = JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
            return strResult;
        }
        #endregion
    }
}
